package com.bluetooth.demo.ui;

import android.app.Fragment;
import android.os.Bundle;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.bluetooth.demo.R;
import com.bluetooth.demo.event.ClearLogEvent;
import com.bluetooth.demo.event.DeviceDataEvent;
import com.bluetooth.demo.event.DeviceInfoEvent;
import com.bluetooth.demo.event.DeviceLogEvent;
import com.bluetooth.demo.utils.DeviceDataUtils;
import com.lifesense.ble.bean.LsDeviceInfo;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

public class DebugFragment extends Fragment {

    private View mCurrentView;
    private TextView logTextView;

    public DebugFragment() {
        // Required empty public constructor
    }

    public static DebugFragment newInstance() {
        DebugFragment fragment = new DebugFragment();
        return fragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        mCurrentView = inflater.inflate(R.layout.fragment_debug, container, false);
        EventBus.getDefault().register(this);
        logTextView = (TextView) mCurrentView.findViewById(R.id.device_log_text_view);
        logTextView.setVisibility(View.VISIBLE);
        logTextView.setText("");

        return mCurrentView;
    }

    public void onDestroyView() {
        super.onDestroyView();
        EventBus.getDefault().unregister(this);
    }
    /**
     * 展示设备详细信息
     *
     * @param device
     */
    private void showDeviceInfo(LsDeviceInfo device) {

        if (getActivity() == null) {
            return;
        }

        logTextView.append("------------------------------------" + "\r\n");
        logTextView.append("deviceName: " + device.getDeviceName() + "\n");
        logTextView.append("broadcastID: " + device.getBroadcastID() + "\n");
        logTextView.append("deviceType: " + device.getDeviceType() + "\n");
        logTextView.append("password: " + device.getPassword() + "\n");
        logTextView.append("deviceID: " + device.getDeviceId() + "\n");
        logTextView.append("deviceSN: " + device.getDeviceSn() + "\n");
        logTextView.append("modelNumber: " + device.getModelNumber() + "\n");
        logTextView.append("firmwareVersion: " + device.getFirmwareVersion() + "\n");
        logTextView.append("hardwareVersion: " + device.getHardwareVersion() + "\n");
        logTextView.append("softwareVersion: " + device.getSoftwareVersion() + "\n");
        logTextView.append("UserNumber: " + device.getDeviceUserNumber() + "\n");
        logTextView.append("protocolType: " + device.getProtocolType() + "\n");

    }

    /**
     * 展示设备的测量数据
     *
     * @param obj
     */
    private void showDeviceMeasuringData(Object obj) {
        if (obj == null || getActivity() == null) {
            return;
        }

        logTextView.append("\r\n");
        logTextView.append(Html.fromHtml("<font color='red'>" + "-----------------------" + "</font>"));
        logTextView.append("\r\n");
        logTextView.append(DeviceDataUtils.formatStringValue(obj.toString()));
        logTextView.append("\r\n");
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(ClearLogEvent event) {

        logTextView.setText("");
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(DeviceInfoEvent event) {

        showDeviceInfo(event.getDeviceInfo());
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(DeviceDataEvent event) {

        showDeviceMeasuringData(event.getObject());
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(DeviceLogEvent event) {

        logTextView.append(event.getText());
    }

}
